import axios from 'axios'
import { isFunction } from 'lodash-es'
// import interceptors from './interceptors'

class VAxios {
  constructor(options) {
    this.options = options
    this.axiosInstance = axios.create(options)
    this.setInterceptors()
  }

  getInstance() {
    return this.axiosInstance
  }

  setHeader(header) {
    if (!this.axiosInstance) {
      return
    }
    Object.assign(this.axiosInstance.defaults.headers, header)
  }

  setInterceptors() {
    const {
      requestInterceptor,
      requestCatchInterceptor,
      responseInterceptor,
      responseCatchInterceptor
    } = this.options.interceptors
    if (requestInterceptor && isFunction(requestInterceptor)) {
      this.axiosInstance.interceptors.request.use(requestInterceptor, undefined)
    }

    if (requestCatchInterceptor && isFunction(requestCatchInterceptor)) {
      this.axiosInstance.interceptors.request.use(undefined, requestCatchInterceptor)
    }

    if (responseInterceptor && isFunction(responseInterceptor)) {
      this.axiosInstance.interceptors.response.use(responseInterceptor, undefined)
    }

    if (responseCatchInterceptor && isFunction(responseCatchInterceptor)) {
      this.axiosInstance.interceptors.response.use(undefined, responseCatchInterceptor)
    }
  }

  get(config, options) {
    return this.request({ ...config, method: 'GET' }, options)
  }

  post(config, options) {
    return this.request({ ...config, method: 'POST' }, options)
  }

  put(config, options) {
    return this.request({ ...config, method: 'PUT' }, options)
  }

  delete(config, options) {
    return this.request({ ...config, method: 'DELETE' }, options)
  }

  uploadFile(config, options) {
    console.log('????')
    const formData = new FormData()
    Object.keys(config.data).forEach((key) => {
      formData.append(key, config.data[key])
    })
    return this.request(
      {
        ...config,
        method: 'POST',
        headers: {
          'Content-Type': 'multipart/form-data'
        },
        data: formData
      },
      options
    )
  }

  request(config, options) {
    console.log('request argument options >>> ', options)
    const { transform } = this.options

    const { beforeRequestHook, requestCatchHook, transformRequestHook } = transform || {}

    if (beforeRequestHook && isFunction(beforeRequestHook)) {
      beforeRequestHook()
    }
    return new Promise((resolve, reject) => {
      this.axiosInstance
        .request(config)
        .then((res) => {
          if (transformRequestHook && isFunction(transformRequestHook)) {
            try {
              const ret = transformRequestHook(res)
              resolve(ret)
            } catch (err) {
              reject(err)
            }
            return
          }
          resolve(res)
        })
        .catch((e) => {
          if (requestCatchHook && isFunction(requestCatchHook)) {
            reject(requestCatchHook(e))
            return
          }
          reject(e)
        })
    })
  }
}

export default VAxios

// const baseURL = import.meta.env.VITE_APP_BASE_URL

// const instance = axios.create({
//   baseURL,
//   timeout: 5000,
//   method: 'POST'
// })

// instance.interceptors.request.use(
//   (config) => {
//     let _config = {}
//     if (interceptors.request) {
//       _config = interceptors.request(config)
//     }

//     if (!config.url.includes('login')) {
//       config.headers.authorization = localStorage.getItem('token')
//     }
//     return Object.assign(config, _config)
//   },
//   (err) => Promise.reject(err)
// )

// instance.interceptors.response.use((res) => {
//   console.log(res)
//   return res.data
// }, undefined)

// export default instance
